package com.ruoyi.web.controller.task;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.task.domain.CheckResult;
import com.ruoyi.task.domain.RiskDetailInfo;
import com.ruoyi.task.domain.SceneCameraParamsInfo;
import com.ruoyi.task.domain.QuerySceneParams;
import com.ruoyi.task.domain.TaskDetail;
import com.ruoyi.task.domain.TaskRiskImage;
import com.ruoyi.task.service.ISceneParamsInfoService;
import com.ruoyi.task.service.ITaskDetailResultService;
import com.ruoyi.task.service.ITaskDetailService;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 提供给分行的视频服务接口
 */
@RestController
@RequestMapping("/branch/video")
public class BranchController extends BaseController {


    @Autowired
    private ITaskDetailResultService taskDetailResultService;

    @Autowired
    private ITaskDetailService taskDetailService;

    @Autowired
    private ISceneParamsInfoService sceneParamsInfoService;

    /**
     * 分行上送场景分析结果数据
     *
     * @param riskDetailInfoList
     * @return
     */
    @PostMapping("sceneCheckResult")
    public AjaxResult sceneCheckResult(@RequestBody List<RiskDetailInfo> riskDetailInfoList) {
        return toAjax(taskDetailResultService.sceneCheckResult(riskDetailInfoList));
    }


    /**
     * 分行上送场景分析结果图片数据
     *
     * @param riskImage
     * @return
     */
    @PostMapping("sceneCheckImage")
    public AjaxResult sceneCheckImage(@RequestBody TaskRiskImage riskImage) {
        return toAjax(taskDetailResultService.sceneCheckImage(riskImage));
    }


    /**
     * 分行更新任务状态
     * @param taskDetail
     * @return
     */
    @PostMapping("updateTaskStatus")
    public AjaxResult updateTaskStatus(@RequestBody TaskDetail taskDetail) {

        logger.info("updateTaskStatus param = {}", JSON.toJSONString(taskDetail));
        return toAjax(taskDetailService.updateTaskDetailStatusByTaskId(taskDetail));
    }


    /**
     * 【总行分析任务】场景检查结果
     * @param checkResult
     * @return
     */
    @PostMapping("checkResult")
    @ResponseBody
    public AjaxResult sceneCheckResult(@RequestBody CheckResult checkResult) {
        boolean result = false;
        try {
            logger.info("main sceneCheckResult taskId = {}, sceneID = {}", checkResult.getTaskId(), checkResult.getSceneId());

            result = taskDetailResultService.sceneCheckResult(checkResult);

            logger.info("main sceneCheckResult result, taskId = {}, sceneId = {}, result = {}",
                    checkResult.getTaskId(), checkResult.getSceneId(), result);
        } catch (Exception e) {
            logger.error("main sceneCheckResult error", e);
        }
        return toAjax(result);
    }


    /**
     * 获取场景参数配置
     * @param sceneCameraParams
     * @return
     */
    @PostMapping("sceneParamsConfig")
    @ResponseBody
    public AjaxResult querySceneParamsConfig(@RequestBody QuerySceneParams sceneCameraParams) {
        try {
            logger.info("querySceneParamsConfig params = {}", JSON.toJSONString(sceneCameraParams));
            if (StringUtils.isBlank(sceneCameraParams.getTaskType())) {
                sceneCameraParams.setTaskType("2");
            }

            if ((StringUtils.equals(sceneCameraParams.getTaskType(), "2") && (
                    StringUtils.isBlank(sceneCameraParams.getEquipmentIp())
                            || StringUtils.isBlank(sceneCameraParams.getEquipmentChannel())
                            || StringUtils.isBlank(sceneCameraParams.getScenariosType())))
                    || ((StringUtils.equals(sceneCameraParams.getTaskType(), "3") || StringUtils.equals(
                            sceneCameraParams.getTaskType(), "1")) && (
                            StringUtils.isBlank(sceneCameraParams.getCameraId())
                                    || StringUtils.isBlank(sceneCameraParams.getScenariosType())))
            ) {
                throw new BaseException("参数为空");
            }

            if (StringUtils.isNotBlank(sceneCameraParams.getTaskId())) {
                TaskDetail taskDetail = taskDetailService.queryTaskDetailByTaskId(
                        sceneCameraParams.getTaskId());
                if (taskDetail == null) {
                    throw new BaseException("未找到taskId相应的任务");
                }
                sceneCameraParams.setCameraId(taskDetail.getCameraId());
                sceneCameraParams.setTaskType(taskDetail.getTaskType());
                sceneCameraParams.setEquipmentIp(taskDetail.getEquipmentIp());
                sceneCameraParams.setEquipmentChannel(taskDetail.getEquipmentChannel());
                sceneCameraParams.setBankCode(taskDetail.getBankCode());
                sceneCameraParams.setScenariosType(sceneCameraParams.getScenariosType());
            }

            SceneCameraParamsInfo paramsInfo = new SceneCameraParamsInfo();
            BeanUtils.copyProperties(paramsInfo, sceneCameraParams);

            Map<String, String> configMap = sceneParamsInfoService.querySceneParamsConfig(
                    paramsInfo);
            logger.info("querySceneParamsConfig result = {}", JSON.toJSONString(configMap));

            return AjaxResult.success("操作成功", configMap);
        } catch (BaseException e) {
            logger.error("querySceneParamsConfig error", e);
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("querySceneParamsConfig error", e);
        }
        return AjaxResult.error("系统错误，请稍后再试！");
    }



}
